const web3 = new Web3(Web3.givenProvider);
//账户
let Wc_accounts = [];

// web3.eth.getAccounts().then(console.log);
//获取账户余额
async function Wc_getAccounts() {

    await web3.eth.getAccounts().then(function(accounts) {
        Wc_accounts = accounts
        console.log(Wc_accounts)
    });
    console.log("wc_accounts[0]", Wc_accounts[0]);
}

const Wc_abi = [{
        "inputs": [{
                "internalType": "int256",
                "name": "x",
                "type": "int256"
            },
            {
                "internalType": "int256",
                "name": "y",
                "type": "int256"
            }
        ],
        "name": "add",
        "outputs": [{
            "internalType": "int256",
            "name": "",
            "type": "int256"
        }],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [{
            "internalType": "int8[2]",
            "name": "x",
            "type": "int8[2]"
        }],
        "name": "sub",
        "outputs": [{
            "internalType": "int256",
            "name": "",
            "type": "int256"
        }],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "stateMutability": "payable",
        "type": "fallback"
    },
    {
        "stateMutability": "payable",
        "type": "receive"
    },
    {
        "inputs": [],
        "name": "count",
        "outputs": [{
            "internalType": "int256",
            "name": "",
            "type": "int256"
        }],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [{
            "internalType": "uint256",
            "name": "x",
            "type": "uint256"
        }],
        "name": "testRequire",
        "outputs": [],
        "stateMutability": "pure",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "testReturn",
        "outputs": [{
                "internalType": "uint8[3]",
                "name": "",
                "type": "uint8[3]"
            },
            {
                "internalType": "bool",
                "name": "",
                "type": "bool"
            },
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            },
            {
                "components": [{
                        "internalType": "string",
                        "name": "str",
                        "type": "string"
                    },
                    {
                        "internalType": "address",
                        "name": "add",
                        "type": "address"
                    },
                    {
                        "internalType": "bool",
                        "name": "boo",
                        "type": "bool"
                    },
                    {
                        "internalType": "uint256",
                        "name": "time",
                        "type": "uint256"
                    }
                ],
                "internalType": "struct Counter.myStruct",
                "name": "",
                "type": "tuple"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    }
];
const Wc_ERC20abi = [{
        "inputs": [{
                "internalType": "string",
                "name": "name",
                "type": "string"
            },
            {
                "internalType": "string",
                "name": "symbol",
                "type": "string"
            }
        ],
        "stateMutability": "nonpayable",
        "type": "constructor"
    },
    {
        "anonymous": false,
        "inputs": [{
                "indexed": true,
                "internalType": "address",
                "name": "owner",
                "type": "address"
            },
            {
                "indexed": true,
                "internalType": "address",
                "name": "spender",
                "type": "address"
            },
            {
                "indexed": false,
                "internalType": "uint256",
                "name": "value",
                "type": "uint256"
            }
        ],
        "name": "Approval",
        "type": "event"
    },
    {
        "anonymous": false,
        "inputs": [{
                "indexed": true,
                "internalType": "address",
                "name": "from",
                "type": "address"
            },
            {
                "indexed": true,
                "internalType": "address",
                "name": "to",
                "type": "address"
            },
            {
                "indexed": false,
                "internalType": "uint256",
                "name": "value",
                "type": "uint256"
            }
        ],
        "name": "Transfer",
        "type": "event"
    },
    {
        "inputs": [{
                "internalType": "address",
                "name": "owner",
                "type": "address"
            },
            {
                "internalType": "address",
                "name": "spender",
                "type": "address"
            }
        ],
        "name": "allowance",
        "outputs": [{
            "internalType": "uint256",
            "name": "",
            "type": "uint256"
        }],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [{
                "internalType": "address",
                "name": "spender",
                "type": "address"
            },
            {
                "internalType": "uint256",
                "name": "amount",
                "type": "uint256"
            }
        ],
        "name": "approve",
        "outputs": [{
            "internalType": "bool",
            "name": "",
            "type": "bool"
        }],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [{
            "internalType": "address",
            "name": "account",
            "type": "address"
        }],
        "name": "balanceOf",
        "outputs": [{
            "internalType": "uint256",
            "name": "",
            "type": "uint256"
        }],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "decimals",
        "outputs": [{
            "internalType": "uint8",
            "name": "",
            "type": "uint8"
        }],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [{
                "internalType": "address",
                "name": "spender",
                "type": "address"
            },
            {
                "internalType": "uint256",
                "name": "subtractedValue",
                "type": "uint256"
            }
        ],
        "name": "decreaseAllowance",
        "outputs": [{
            "internalType": "bool",
            "name": "",
            "type": "bool"
        }],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "getDEcimals",
        "outputs": [{
            "internalType": "uint256",
            "name": "",
            "type": "uint256"
        }],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [{
                "internalType": "address",
                "name": "spender",
                "type": "address"
            },
            {
                "internalType": "uint256",
                "name": "addedValue",
                "type": "uint256"
            }
        ],
        "name": "increaseAllowance",
        "outputs": [{
            "internalType": "bool",
            "name": "",
            "type": "bool"
        }],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "name",
        "outputs": [{
            "internalType": "string",
            "name": "",
            "type": "string"
        }],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "symbol",
        "outputs": [{
            "internalType": "string",
            "name": "",
            "type": "string"
        }],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "totalSupply",
        "outputs": [{
            "internalType": "uint256",
            "name": "",
            "type": "uint256"
        }],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [{
                "internalType": "address",
                "name": "to",
                "type": "address"
            },
            {
                "internalType": "uint256",
                "name": "amount",
                "type": "uint256"
            }
        ],
        "name": "transfer",
        "outputs": [{
            "internalType": "bool",
            "name": "",
            "type": "bool"
        }],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [{
                "internalType": "address",
                "name": "from",
                "type": "address"
            },
            {
                "internalType": "address",
                "name": "to",
                "type": "address"
            },
            {
                "internalType": "uint256",
                "name": "amount",
                "type": "uint256"
            }
        ],
        "name": "transferFrom",
        "outputs": [{
            "internalType": "bool",
            "name": "",
            "type": "bool"
        }],
        "stateMutability": "nonpayable",
        "type": "function"
    }
];
const Wc_myContract_add = '0xb141AE441cF5f1Bc96987780E80e210E9220a880'

const Wc_ERC20_add = '0xA89f52Ed4a92B1C0364e889fec65a2d5f3fD3b78'

var Wc_myContract = new web3.eth.Contract(Wc_abi, Wc_myContract_add);

var Wc_ERC20 = new web3.eth.Contract(Wc_ERC20abi, Wc_ERC20_add)

async function Wc_newtian() {
    await Wc_getAccounts();
    var Wc_tianbt = $("#tianbt").val()
    var Wc_tianneirong = $("#tianneirong").val()
    var Wc_tiantime = $("#tiantime").val()
    var Wc_tianfeiyon = $("#tianfeiyon").val()

    Wc_myContract.methods.Wc_createtian(Wc_tianbt, Wc_tianneirong, Wc_tiantime, Wc_tianfeiyon).send({ from: Wc_accounts[0] }).then((result) => {
        if (result = null) {
            alert("提案失败")
        } else {
            Wc_myContract.methods.Wc_returnid().call().then(function(x) {
                alert("success:  " + x.slice(-1));
            })
        }
    })
}

//投票
async function Wc_toupiao() {
    await Wc_getAccounts();

    console.log("account", Wc_accounts[0])

    //判断附议费用是否足够
    Wc_myContract.methods.testReturn().call().then(function(z) {
        if (z[1])
            console.log("xxxxxxz", z[1])

    })

}
//查询
function Wc_chaxun() {
    var Wc_chaxunid = $("#chaxunid").val()
    Wc_myContract.methods.Wc_Checktian(Wc_chaxunid).call(
        function(s) {
            if (s) {
                alert(s)
            } else {
                Wc_myContract.methods.Wc_gettianName(Wc_chaxunid).call().then(
                    function(x) {
                        document.getElementById("chaxunName").innerHTML = '提案名称:' + x


                    }
                )
                Wc_myContract.methods.Wc_gettiannr(Wc_chaxunid).call().then(
                    function(x) {
                        document.getElementById("chaxunnr").innerHTML = '提案内容:' + x
                    }
                )
                Wc_myContract.methods.Wc_gettianVN(Wc_chaxunid).call().then(
                    function(x) {
                        document.getElementById("tprs").innerHTML = '投票人数:' + x
                    }
                )
                Wc_myContract.methods.Wc_getendTime(Wc_chaxunid).call().then(
                    function(x) {
                        document.getElementById("endtime").innerHTML = '结束时间:' + x
                    }
                )
                Wc_myContract.methods.Wc_gettianmoney(Wc_chaxunid).call().then(
                    function(x) {
                        document.getElementById("feiyonneed").innerHTML = '提案所需费用:' + x
                    }
                )
                Wc_myContract.methods.Wc_getvotemoney(Wc_chaxunid).call().then(
                    function(x) {
                        document.getElementById("feiyonsum").innerHTML = '已附议费用:' + x
                    }
                )
            }
        }
    );
}

//查询已有提案id
function Wc_chaxunsum() {
    Wc_myContract.methods.Wc_returnid().call().then(
        function(x) {
            document.getElementById("tianids").innerHTML = '提案ids:' + x
            console.log(x)
        }
    )
}

//查证提案
function Wc_chazheng() {
    var Wc_chazhenid = $("#chazhengid").val()
        // var Wc_voterAddress = $("#tianneirong").val()
    Wc_myContract.methods.Wc_Checktian(Wc_chazhenid).call().then((s) => {
        alert(s);
    })
}

//取款
async function Wc_getmoney() {
    await Wc_getAccounts();
    var Wc_getMoney = $("#getmoney").val()
    Wc_myContract.methods.Wc_approvesend(Wc_ERC20_add, Wc_accounts[0], Wc_getMoney).send({ from: Wc_accounts[0] }).on('receipt', function() {
        alert("取款成功");
    })
}